package com.alibaba.citrus.cr.price.ability.functions.factory.activity;

import com.alibaba.citrus.cr.price.ability.functions.factory.activity.process.ActivityProcessManager;
import com.alibaba.citrus.cr.price.repository.ActivityQueryRepository;
import com.alibaba.citrus.cr.common.annotation.FunctionInvoker;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityTermEntity;
import com.alibaba.citrus.cr.price.ability.functions.entity.activity.ActivityTitleEntity;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crpricecenter.dto.request.*;
import com.epoch.app.crpricecenter.sdo.ActivityTermSDO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author: qiaojiange
 * @create: 2021/10/17
 * @Description 活动项目工厂
 */
public class ActivityTermFactory {
    private static Log log = Log.getLogger(ActivityTermFactory.class);

    private static ActivityQueryRepository queryRepository = SpringContextUtil.getBean(ActivityQueryRepository.class);

    private static ActivityProcessManager manager = SpringContextUtil.getBean(ActivityProcessManager.class);

    public static List<ActivityTermEntity> create(ActivityTitleEntity titleEntity, ActivityInfoRequest request) {
        return manager.getActivityProcess(titleEntity.obtainFacadeCode())
                .addInfo(titleEntity, request.getItemList().get(0).getItemId(),request.getItemList().get(0).getSalesUnit());
    }


    /**
     * 根据活动termId,查询活动项目实体
     *
     * @param request
     * @param titleEntity
     * @return
     */
    @FunctionInvoker(printLog = true)
    public static List<ActivityTermEntity> create(ParticipateActivityInfoRequest request, ActivityTitleEntity titleEntity) {
        log.info("ActivityTermFactory#create request:{}; titleEntity:{}", JSON.toJSONString(request), JSON.toJSONString(titleEntity));
        List<ActivityTermEntity> result = Lists.newArrayList();
        ActivityItemInfo itemInfo = request.getParticipateActivityTermRequest().getItem();
        List<ActivityTermEntity> activityTermEntities = manager.getActivityProcess(titleEntity.obtainFacadeCode()).
                addInfo(titleEntity, itemInfo.getItemId(),itemInfo.getSalesUnit());
        log.info("ActivityTermFactory#create activityTermEntities:{}", JSON.toJSONString(activityTermEntities));
        for (ActivityTermEntity activityTermEntity : activityTermEntities) {
            if (StringUtils.equals(activityTermEntity.obtainActivityTermId(),
                    request.getParticipateActivityTermRequest().getActivityTermId())) {
                result.add(activityTermEntity);
                break;
            }
        }
        log.info("ActivityTermFactory#create result:{}", JSON.toJSONString(result));
        return result;
    }


    public static ActivityTermEntity create(ActivityDetailQueryViewRequest request) {
        ActivityTermSDO activityTermSDO = new ActivityTermSDO();
        activityTermSDO.setActivityId(request.getActivityId());
        ActivityTermEntity result = ActivityTermEntity.of(activityTermSDO);
        return result;
    }


    public static ActivityTermEntity create(ActivityQueryViewRequest request) {

        ActivityTermSDO activityTermSDO = new ActivityTermSDO();
        BeanUtils.copyProperties(request,activityTermSDO);
        activityTermSDO.setStart(request.getStart());
        activityTermSDO.setLimit(request.getLimit());
        ActivityTermEntity result = ActivityTermEntity.of(activityTermSDO);
        return result;
    }

    public static ActivityTermEntity create(ActivityTermSDO sdo) {
        ActivityTermEntity result = ActivityTermEntity.of(sdo);
        return result;
    }
}
